
// 引入AES.js
var baseJs = require("../../common/base.js");
// 二维码绘制器
var QR = require("../../common/qrcode.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imagePath: "",
    url: "",
    // 
    uid: "",
    userType: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      uid: wx.getStorageSync('uid'),
      userType: wx.getStorageSync('userType')
    })
    this.GetBusinessShopInfo();
    //动态设置画布大小
    var size = this.setCanvasSize();
  },

  // GetBusinessShopInfo
  GetBusinessShopInfo: function () {
    var that = this;
    var uid = this.data.uid;
    var url = this.data.url;
    wx.request({
      url: baseJs.api_User + "GetBusinessShopInfo/" + uid,
      success: function (res) {
        var json = res.data.ResultData;
        console.log(json);
        if(json){
          that.setData({
            url: "eee2016&" + uid + "&" + json.BusinessAllName,
          })
          that.createCodeImg();
        }else{
          wx.showToast({
            title: '失败',
            icon: 'error',
            duration: 2000
          })
        }
      }
    })
  },
  //适配不同屏幕大小的canvas
  setCanvasSize: function () {
    var size = {};
    try {
      var res = wx.getSystemInfoSync();
      var scale = 750 / 400; //不同屏幕下canvas的适配比例；设计稿是750宽
      //canvas画布为正方形
      var width = res.windowWidth / scale;
      var height = width;
      size.w = width;
      size.h = height;
    } catch (e) {
      console.log("获取设备信息失败" + e);
    }
    return size;
  },
  //获取临时缓存照片路径，存入data中
  canvasToTempImage: function () {
    var that = this;
    wx.canvasToTempFilePath({
      canvasId: 'mycanvas',
      success: function (res) {
        var tempFilePath = res.tempFilePath;
        console.log(tempFilePath);
        that.setData({
          imagePath: tempFilePath,
          // canvasHidden:true
        });
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  //点击图片进行预览，长按保存分享图片
  previewImg: function (e) {
    var img = this.data.imagePath;
    console.log(img);
    wx.previewImage({
      current: img, // 当前显示图片的http链接
      urls: [img] // 需要预览的图片http链接列表
    })
  },
  // 
  createQrCode: function (url, canvasId, cavW, cavH) {
    //调用插件中的draw方法，绘制二维码图片
    QR.api.draw(url, canvasId, cavW, cavH);
    setTimeout(() => { this.canvasToTempImage(); }, 1000);
  },
  // createCodeImg
  createCodeImg: function () {
    var that = this;
    var url = this.data.url;
    console.log(url);
    wx.showToast({
      title: '生成中...',
      icon: 'loading',
      duration: 2000
    });
    var st = setTimeout(function () {
      wx.hideToast()
      var size = that.setCanvasSize();
      //绘制二维码
      that.createQrCode(url, "mycanvas", size.w, size.h);
      clearTimeout(st);
    }, 2000)
  }
})